// pages/shop/shop.js
const app = getApp()
const util = require('../../utils/util.js');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        //页面颜色
        pageColor: app.pageColor,
        storageDataArr: [],
        //商品合计价格
        goodsPrice: 0,
        //全选按钮状态
        allState: 0,
        // 编辑还是完成0编辑 1完成
        handleState: 0
    },

    /**
     * 跳转首页
     */

    toHome: function () {
        wx.switchTab({
            url: '../index/index'
        })
    },
    /**
     * 编辑
     */
    onTapEdit: function () {
        this.setData({
            handleState: 1
        })
    },

    /**
     * 完成
     */
    onTapDone: function () {
        this.setData({
            handleState: 0
        })
    },

    /**
     * 结算商品
     */
    onTapAccount: function () {
        var myDate = new Date().getTime();//获取系统当前时间
        var allSlectGoodsArr = [];
        var positionArr = this.getAllSlectGoodsPosition();
        if (positionArr.length != 0) {
            for (var i = 0; i < positionArr.length; i++) {
                allSlectGoodsArr.push(this.data.storageDataArr[positionArr[i]])
            }
            for (let index = 0; index < allSlectGoodsArr.length; index++) {
                const element = allSlectGoodsArr[index];
                if( element.isSpike ==1){
                    if(myDate>element.endAt*1000 || myDate<element.startAt*1000){
                        util.failToast("有未开抢的商品!");
                        return;
                    }
                   
                }
            }
            console.log("111111")
            this.navigateTo("../firmOrder/firmOrder?goodInfoArr=" + JSON.stringify(allSlectGoodsArr))
            return;
        }
        util.failToast("请选择商品!")
    },
    /**
    * 页面跳转
    */

    navigateTo: function (url) {
        wx.navigateTo({
            url: url
        })
    },

    /**
     * 删除商品
     */
    onTapRemove: function () {
        var positionArr = this.getAllSlectGoodsPosition()
        if (positionArr.length != 0) {
            util.showModal("", "确认删除商品吗?", this.confirmCallBack, this.cancelCallBack)
            return;
        }
        util.failToast("请选择商品!")
    },

    /**
     * 确认删除
     */
    confirmCallBack: function () {
        var positionArr = this.getAllSlectGoodsPosition();
        var storageDataArr = this.data.storageDataArr;
        var length = positionArr.length;
        for (var i = 0; i < positionArr.length; i++) {
            storageDataArr.splice(positionArr[i], positionArr.length);
        }
        this.setData({
            storageDataArr: storageDataArr
        })

        this.calculateTotalPrice();
        //更新缓存
        util.setStorage("selectGoodInfoArr", storageDataArr);
    },

    /**
     * 取消删除
     */
    cancelCallBack: function () {

    },

    /**
     * 遍历所有选中的商品的position
     */
    getAllSlectGoodsPosition: function () {
        var storageDataArr = this.data.storageDataArr;
        var positionArr = [];
        for (var i = 0; i < storageDataArr.length; i++) {
            if (storageDataArr[i].state == 1) {
                positionArr.push(i);
            }
        }
        return positionArr;
    },

    /**
     * 计算选中商品总价格
     */
    calculateTotalPrice: function () {
        var storageDataArr = this.data.storageDataArr;
        //获取所有选中的商品的position
        var positionArr = this.getAllSlectGoodsPosition();

        var goodsTotalPrice = 0;
        for (var i = 0; i < positionArr.length; i++) {
            goodsTotalPrice += storageDataArr[positionArr[i]].goodPrice * storageDataArr[positionArr[i]].goodCount
        }
        this.setData({
            goodsPrice: goodsTotalPrice
        })

    },
    /**
     * 选择单个商品
     */
    onTapItemSlect: function (options) {
        var pos = options.currentTarget.dataset.position;
        this.data.storageDataArr[pos].state = 1
        this.setData({
            storageDataArr: this.data.storageDataArr
        })
        this.setData({
            allState: this.isAllSlectGoods() ? 1 : 0
        })
        //计算商品价格
        this.calculateTotalPrice();
    },

    /**
     * 取消选择单个商品
     */
    onTapItemCancel: function (options) {
        var pos = options.currentTarget.dataset.position;
        this.data.storageDataArr[pos].state = 0
        this.setData({
            storageDataArr: this.data.storageDataArr
        })
        this.setData({
            allState: this.isAllSlectGoods() ? 1 : 0
        })
        //计算商品价格
        this.calculateTotalPrice();
    },

    /**
     * 是否商品全部选中
     */
    isAllSlectGoods: function () {
        var storageDataArr = this.data.storageDataArr;
        var positionArr = this.getAllSlectGoodsPosition();
        return positionArr.length == storageDataArr.length

    },

    //全选
    onTapSelectAll: function () {
        var storageDataArr = this.data.storageDataArr;
        this.setData({
            allState: 1
        })
        for (var i = 0; i < storageDataArr.length; i++) {
            storageDataArr[i].state = 1
        }
        this.setData({
            storageDataArr: storageDataArr
        })

        //计算商品价格
        this.calculateTotalPrice();
    },

    //取消全选
    onTapcancelSelectAll: function () {
        var storageDataArr = this.data.storageDataArr;
        this.setData({
            allState: 0
        })

        for (var i = 0; i < storageDataArr.length; i++) {
            storageDataArr[i].state = 0
        }
        this.setData({
            storageDataArr: storageDataArr
        })


        //计算商品价格
        this.calculateTotalPrice();
    },

    //减少数量
    subtract: function (options) {
        var pos = options.currentTarget.dataset.position;
        if (this.data.storageDataArr[pos].goodCount <= 1) {
            return;
        }
        this.data.storageDataArr[pos].goodCount -= 1;
        this.setData({
            storageDataArr: this.data.storageDataArr
        })

        //如果该商品没被选择不用计算价格
        if (this.data.storageDataArr[pos].state == 1) {
            //计算商品价格
            this.calculateTotalPrice();
        }


    },
    //增加数量
    add: function (options) {
        var pos = options.currentTarget.dataset.position;
        this.data.storageDataArr[pos].goodCount += 1;
        this.setData({
            storageDataArr: this.data.storageDataArr
        })
        //如果该商品没被选择不用计算价格
        if (this.data.storageDataArr[pos].state == 1) {
            //计算商品价格
            this.calculateTotalPrice();
        }

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        util.setNavigationBarTitle("购物车");
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var storageDataArr = util.getStorage("selectGoodInfoArr")
        
        var myDate = new Date();//获取系统当前时间
        for (let index = 0; index < storageDataArr.length; index++) {
            const element = storageDataArr[index];
            element.currentTimestamp = myDate.getTime();
            element.spikeState = 0;
            if(element.isSpike==1){
                element.currentTimestamp = myDate.getTime();
                //开抢中
                if (element.currentTimestamp >= element.startAt * 1000 &&
                    element.currentTimestamp <= element.endAt * 1000) {
                    element.text = "抢购中"
                    element.spikeState = 1;
                } else if (element.currentTimestamp < element.startAt * 1000) {
                    element.text = "即将开抢"
                    element.spikeState = 2;
                } else if (element.currentTimestamp > element.endAt * 1000) {
                    element.text = "抢购结束"
                    element.spikeState = 3;
                }
                var startAtDate = new Date(element.startAt * 1000)
                var endAtDate = new Date(element.endAt * 1000)
                element.start_at = util.formatTime(startAtDate)
                element.end_at = util.formatTime(endAtDate)
            }
        }
        this.setData({
            storageDataArr: storageDataArr
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        //页面切换的时候回复默认状态
        this.setData({
            allState: 0,
            goodsPrice: 0
        })

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})